package com.hd.trans.widgets;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import com.hd.trans.utils.IToastView;


/**
 *
 * Author: yang
 * Date: 2018/11/13 11:11
 * Desc: 自定义错误提示
 *
 */
public class ErrorTickView extends View implements IToastView {
    private float mDensity = -1;
    private Paint mPaint;
    private final float CONST_RADIUS = dip2px(1.2f);
    private final float CONST_RECT_WEIGHT = dip2px(2);
    private final float CONST_LEFT_RECT_W = dip2px(9);
    private final float CONST_RIGHT_RECT_W = dip2px(15);
    private final float MIN_LEFT_RECT_W = dip2px(2.2f);
    private final float MAX_RIGHT_RECT_W = CONST_RIGHT_RECT_W + dip2px(3.7f);

    private float mMaxLeftRectWidth;
    private float mLeftRectWidth;
    private float mRightRectWidth;
    private boolean mLeftRectGrowMode;

    public ErrorTickView(Context context) {
        super(context);
        init();
    }

    public ErrorTickView(Context context, AttributeSet attrs){
        super(context,attrs);
        init();
    }

    private void init () {
        mPaint = new Paint();
        mPaint.setColor( Color.parseColor("#FFFFFF") );
        mLeftRectWidth = CONST_LEFT_RECT_W;
        mRightRectWidth = CONST_RIGHT_RECT_W;
        mLeftRectGrowMode = false;
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        int totalW = getWidth();
        int totalH = getHeight();
        // rotate canvas first
        canvas.rotate(45, totalW / 2, totalH / 2);



//        totalW /= 1.2;
//        totalH /= 1.4;
        mMaxLeftRectWidth = (totalW + CONST_LEFT_RECT_W) / 2 ;

//        mMaxLeftRectWidth = totalW;


        RectF leftRect = new RectF();
        if (mLeftRectGrowMode) {

            leftRect.left = (totalW - mMaxLeftRectWidth)/2;
            leftRect.right = leftRect.left + mLeftRectWidth;
            leftRect.top = (totalH - CONST_RECT_WEIGHT) / 2;
            leftRect.bottom = (totalH + CONST_RECT_WEIGHT) / 2;
//            canvas.drawRoundRect(leftRect, CONST_RADIUS, CONST_RADIUS, mPaint);
            canvas.drawRoundRect(leftRect, 0, 0, mPaint);

        } else {

            leftRect.left = (totalW - mMaxLeftRectWidth)/2;
            leftRect.right = leftRect.left + mLeftRectWidth;
            leftRect.top = (totalH - CONST_RECT_WEIGHT) / 2;
            leftRect.bottom = (totalH + CONST_RECT_WEIGHT) / 2;
//            canvas.drawRoundRect(leftRect, CONST_RADIUS, CONST_RADIUS, mPaint);
            canvas.drawRoundRect(leftRect, 0, 0, mPaint);

            RectF rightRect = new RectF();
            rightRect.top = (totalH - mMaxLeftRectWidth)/2;
            rightRect.bottom = rightRect.top + mRightRectWidth;
            rightRect.left = (totalW - CONST_RECT_WEIGHT) / 2;
            rightRect.right = (totalW + CONST_RECT_WEIGHT) / 2;

//            canvas.drawRoundRect(rightRect, CONST_RADIUS, CONST_RADIUS, mPaint);
            canvas.drawRoundRect(rightRect, 0, 0, mPaint);

        }


    }

    public float dip2px(float dpValue) {
        if(mDensity == -1) {
            mDensity = getResources().getDisplayMetrics().density;
        }
        return dpValue * mDensity + 0.5f;
    }

    public void startTickAnim () {
        // hide tick
        mLeftRectWidth = 0;
        mRightRectWidth = 0;
        invalidate();
        Animation tickAnim = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                super.applyTransformation(interpolatedTime, t);
                if (0.08f < interpolatedTime && 0.54f >= interpolatedTime) {  // grow left and right rect to right
                    mLeftRectGrowMode = true;
                    mLeftRectWidth = mMaxLeftRectWidth * ((interpolatedTime - 0.08f) / 0.46f);
                    if (0.65 < interpolatedTime) {
                        mRightRectWidth = 0;
                    }
                    invalidate();
                }
                else if (0.54f < interpolatedTime && 1 >= interpolatedTime) { // restore left rect width, shorten right rect to const
                    mLeftRectGrowMode = false;
                    mLeftRectWidth = mMaxLeftRectWidth;
//                    mLeftRectWidth = MIN_LEFT_RECT_W + (CONST_LEFT_RECT_W - MIN_LEFT_RECT_W) * ((interpolatedTime - 0.54f) / 0.46f);
//                    mRightRectWidth = CONST_RIGHT_RECT_W + (MAX_RIGHT_RECT_W - CONST_RIGHT_RECT_W) * (1 - ((interpolatedTime - 0.54f) / 0.46f));
                    mRightRectWidth = mMaxLeftRectWidth * ((interpolatedTime - 0.54f) / 0.46f);
                    invalidate();
                }
            }
        };
        tickAnim.setDuration(600);
        tickAnim.setStartOffset(50);
        startAnimation(tickAnim);
    }

    @Override
    public void cancel() {
        clearAnimation();
    }

}